package com.lgy.ms.service.service.business;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import com.lgy.ms.api.business.IAppProjectService;
import com.lgy.ms.core.bean.RequestParams;
import com.lgy.ms.core.bean.ResponseParams;
import com.lgy.ms.core.dto.ProjectDTO;
import com.lgy.ms.core.enums.CommonEnum;
import com.lgy.ms.core.exception.BusinessException;
import com.lgy.ms.core.utils.SysHelper;
import com.lgy.ms.service.dao.ProjectDao;
import com.lgy.ms.service.service.base.AppBaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service(value = "appProjectService")
public class AppProjectServiceImpl extends AppBaseService implements IAppProjectService {

    @Resource
    private ProjectDao projectDao;

    @Override
    public ResponseParams<Object> add(RequestParams requestParams) throws BusinessException {
        ResponseParams<Object> response = buildResponseParams(requestParams);
        try {
            ProjectDTO projectDTO = super.paramToObject(requestParams, ProjectDTO.class);
            ProjectDTO record = projectDao.selectByProjectName(projectDTO);
            if (record != null) {
                throw new BusinessException("已存在项目--项目名称：" + projectDTO.getProject() + "，所属村镇：" + projectDTO.getTown());
            }
            projectDTO.setProjectDate(projectDTO.getProjectDate().replace("-", ""));
            projectDTO.setProjectId(SysHelper.getUUID("PJ_", 32));
            projectDTO.setCreateDate(DateUtil.date());
            projectDTO.setDeleted(CommonEnum.Normal.value);
            projectDao.insert(projectDTO);
            return finish(response);
        } catch (Exception e) {
            throw super.processException(e, e.getMessage());
        }
    }

    @Override
    public ResponseParams<Object> edit(RequestParams requestParams) throws BusinessException {
        ResponseParams<Object> response = buildResponseParams(requestParams);
        try {
            ProjectDTO projectDTO = super.paramToObject(requestParams, ProjectDTO.class);
            projectDao.updateByPrimaryKeySelective(projectDTO);
            return finish(response);
        } catch (Exception e) {
            throw super.processException(e, e.getMessage());
        }
    }

    @Override
    public ResponseParams<Object> delete(RequestParams requestParams) throws BusinessException {
        ResponseParams<Object> response = buildResponseParams(requestParams);
        try {
            ProjectDTO projectDTO = super.paramToObject(requestParams, ProjectDTO.class);
            projectDao.deleteByPrimaryKey(projectDTO.getProjectId());
            return finish(response);
        } catch (Exception e) {
            throw super.processException(e, e.getMessage());
        }
    }

    @Override
    public ResponseParams<Object> queryByPage(RequestParams requestParams) throws BusinessException {
        try {
            ProjectDTO projectDTO = super.paramToObject(requestParams, ProjectDTO.class);
            List<ProjectDTO> list = projectDao.queryByPage(projectDTO);
            ResponseParams<Object> response = getPage(requestParams, list);
            return finish(response);
        } catch (Exception e) {
            throw super.processException(e, e.getMessage());
        }
    }

    @Override
    public PageInfo<ProjectDTO> getProjectList(Map<String, Object> reqMap) {
        startPage(reqMap);
        ProjectDTO projectDTO = BeanUtil.mapToBean(reqMap, ProjectDTO.class, true);
        List<ProjectDTO> list =  projectDao.queryByPage(projectDTO);
        return new PageInfo<>(list);
    }
}
